package com.example.wx.other;

import com.alibaba.fastjson.JSON;
import com.vivo.it.vwkc.pop.web.common.enums.BizErrorEnum;
import com.vivo.it.vwkc.pop.web.common.enums.RequestResultEnum;
import com.vivo.it.vwkc.pop.web.common.exception.BaseErrorInterface;
import lombok.Data;

/**
 * @author 姜冲/72152416
 * @version 1.0
 * @date 2021/9/27 10:05
 * @description 推荐使用 com.vivo.it.vwkc.inventory.center.api.core.dto.common.response.ResponseDTO
 */
@Data
public class ResponseDTO<T> {
    /**
     * 响应代码
     */
    private String code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 相应时间
     */
    private Long duration;

    /**
     * 响应结果
     */
    private T data;

    public ResponseDTO() {
    }

    public ResponseDTO(BaseErrorInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }

    public boolean succeed(){
        return RequestResultEnum.SUCCESS.getResultCode().equals(code);
    }

    /**
     * 成功
     *
     * @return
     */
    public static <T> ResponseDTO<T> success() {
        return success(null);
    }

    /**
     * 成功
     *
     * @param data
     * @return
     */
    public static <T> ResponseDTO<T> success(T data) {
        ResponseDTO<T> rb = new ResponseDTO<>();
        rb.setCode(RequestResultEnum.SUCCESS.getResultCode());
        rb.setMessage(RequestResultEnum.SUCCESS.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     */
    public static <T> ResponseDTO<T> error(BaseErrorInterface errorInfo) {
        ResponseDTO<T> rb = new ResponseDTO<>();
        rb.setCode(errorInfo.getResultCode());
        rb.setMessage(errorInfo.getResultMsg());
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static <T> ResponseDTO<T> error(String code, String message) {
        ResponseDTO<T> rb = new ResponseDTO<>();
        rb.setCode(code);
        rb.setMessage(message);
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static <T> ResponseDTO<T> error(String message) {
        ResponseDTO<T> rb = new ResponseDTO<>();
        rb.setCode("-1");
        rb.setMessage(message);
        rb.setData(null);
        return rb;
    }

    /**
     * 失败
     */
    public static <T> ResponseDTO<T> error(BizErrorEnum bizErrorEnum, T data) {
        ResponseDTO<T> rb = new ResponseDTO<>();
        rb.setCode(bizErrorEnum.getResultCode());
        rb.setMessage(bizErrorEnum.getResultMsg());
        rb.setData(data);
        return rb;
    }

    /**
     * 失败
     */
    public static <T> ResponseDTO<T> error(BizErrorEnum bizErrorEnum) {
        ResponseDTO<T> rb = new ResponseDTO<>();
        rb.setCode(bizErrorEnum.getResultCode());
        rb.setMessage(bizErrorEnum.getResultMsg());
        rb.setData(null);
        return rb;
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
